package com.person.ntv.service.sys.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.person.common.PageParams;
import com.person.exception.ServiceException;
import com.person.ntv.dao.sys.SysUserMapper;
import com.person.ntv.entity.sys.SysUser;
import com.person.ntv.service.base.impl.BaseServiceImpl;
import com.person.ntv.service.sys.CommonService;
import com.person.ntv.service.sys.SysUserService;
@Service("sysUserService")
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService{
	@Autowired
   private SysUserMapper sysUserMapper;
	@Autowired
   private CommonService commonService;
   @Autowired
   public void setBaseMapper(){
	   super.setBaseMapper(this.sysUserMapper);
   }

@Override
public int deleteByPrimaryKeys(Integer[] ids) {
	try{
		int result=0;
	    for(Integer id:ids){
	    	result+=deleteByPrimaryKey(BigDecimal.valueOf(id));
	    	//删除用户所属角色
	    	sysUserMapper.deleteRoleList(id);
	    }
	    return result;
		}catch (Exception e) {
	          e.printStackTrace();
	          throw new ServiceException("系统错误。");
	  }
}

@Override
public Page<Map<String,Object>> getUserPage(PageParams params)
		throws ServiceException {
	try{
		PageHelper.startPage(params.getPage(),params.getRows(),params.getOrderStr(null));
		return  sysUserMapper.selectPage(params.getParams());
		}catch (Exception e) {
	          e.printStackTrace();
	          throw new ServiceException("系统错误。");
	  } 
}

@Override
public int addUser(SysUser user) throws ServiceException {
	 try {
	  int result=insertSelective(user);
	  //保存对应角色
	  List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
	  if(user.getRoleList()!=null){
	  for(String roleId:user.getRoleList().split(",")){
		  Map<String,Object> map=new HashMap<String,Object>();
		  map.put("userId",user.getId());
		  map.put("roleId",roleId);
		  list.add(map);
	  }
	  sysUserMapper.addRoleList(list);
	  }
	  return result;
	} catch (Exception e) {
	   e.printStackTrace();
	   throw new ServiceException("系统错误。");
	}
}

@Override
public int updateUser(SysUser user) throws ServiceException {
try {
     //更新对应角色
	  sysUserMapper.deleteRoleList(Integer.valueOf(user.getId().toString()));
	  List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
	  if(user.getRoleList()!=null){
	  for(String roleId:user.getRoleList().split(",")){
		  Map<String,Object> map=new HashMap<String,Object>();
		  map.put("userId",user.getId());
		  map.put("roleId",roleId);
		  list.add(map);
	  }
	  sysUserMapper.addRoleList(list);
	  }
      return	updateByPrimaryKeySelective(user);
	} catch (Exception e) {
	   e.printStackTrace();
	   throw new ServiceException("系统错误。");
	}
}

@Override
public List<Map<String, Object>> getUserRoleList(Integer userId)
		throws ServiceException {
	try {
		return sysUserMapper.getUserRoleList(userId);
	} catch (Exception e) {
		e.printStackTrace();
		throw new ServiceException("系统错误。");
	}
}
  
}
